#include "../connection.h"
#include "../protocol.h"
#include "messagehandler.h"
#include <string>
#include <sstream>




using namespace std;
namespace client_server{

	MessageHandler::MessageHandler(Connection* conn){
		istringstream iss(readLine(conn), stringstream::in);
		iss >> command;
		string s;
		iss >> s;
		
		while( toInt(s) != protocol::Protocol::COM_END || toInt(s) != protocol::Protocol::ANS_END){
			parameters += s;
			s.clear();
			iss >> s;
		}
		endmark = s;
	}
	int MessageHandler::getCommand(){
		return toInt(command);
	}
	string MessageHandler::getParameters(){
		return parameters;
	}
	string MessageHandler::getEndmark(){
		return endmark;
	}
	string MessageHandler::readLine(Connection* conn){
 		string s;
    		char ch;
    		while ((ch = conn->read()) != '$')  
        		s += ch;
    		return s;
	}
	int toInt(const string &s){
		istringstream iss(s, stringstream::in);
		int i;
		iss >> i;
		return i;
	}

}
